"""
公共请求封装
"""
import requests
import jsonpath
from deepdiff import DeepDiff
from common.commonfunc import read_data_by_yaml, get_config, CommonLog


class CommonRequests(object):
    """
       公共接口请求
    """
    base_url = None
    api_path = None
    env = None
    headers = None
    api_log = None
    api_rep = None

    def __init__(self, api_path, env):
        self.api_path = api_path
        self.env = env
        _config = get_config()
        self.api_log = CommonLog(__name__, log_path=_config['LogPath'])
        self.api_params = None

    def get_common_config(self, api_config_file_name):
        """
               公共配置
        """
        self.api_log.log_info(f'get_common_config-key:{api_config_file_name}')
        common_config = dict(read_data_by_yaml(self.api_path[api_config_file_name]))
        self.api_log.log_info(f'common_config:{common_config}')
        return common_config

    def get_business_config(self, api_name: str, api_config_file_name: str):
        """
                       业务配置
        """
        self.api_log.log_info(f'get_business_config-key:{api_name}-api_file_name:{api_config_file_name}')
        for k, v in self.get_common_config(api_config_file_name).items():
            if k == api_name:
                self.api_log.log_info(f'get_business_config-business_config:{v}')
                return v

    def get_base_url(self):
        """
                               公共url
        """
        for k, v in self.get_common_config('commonurl').items():
            if str(k).find(self.env):
                base_url = v
                self.api_log.log_info(f'get_base_url-base_url:{base_url}')
                return base_url

    def create_request_params(self, method, api_params_data):
        if isinstance(api_params_data, dict):
            if method == 'post':
                self.api_params = api_params_data
            elif method == 'get':
                tmp = []
                for key, value in api_params_data:
                    tmp.append(''.join([key, '&', value]))
            return self.api_params

    def send_request(self, url, method, headers=None, params=None, data=None, cookies=None, **kwargs):
        """
                                       公共发送请求
        """
        self.base_url = self.get_base_url()
        url = self.base_url + url
        self.api_log.log_info(f'send_request-url:{url}')
        self.api_rep = requests.request(url=url, method=method, headers=headers, params=params, data=data,
                                        cookies=cookies, **kwargs)
        return self.api_rep

    def compare_rep(self, rep, compared_dict):
        df = DeepDiff(rep, compared_dict, ignore_order=True, exclude_paths=["root['data']['token']", "root['data'][" 
                                                                                                     "'expires_time']"])
        self.api_log.log_info(f'df:{df}')
        if len(df.tree)>0:
            values_changed = df['values_changed']
            self.api_log.log_info(f'验证不通过,结果如下：{values_changed}')
        else:
           self.api_log.log_info(f'验证通过')

    def get_json_content(self, json_obj, exp):
        """
                                       响应数据json化
        """

        result = jsonpath.jsonpath(json_obj, exp)
        self.api_log.log_info(f'df:{result}')
        return result
